﻿using AiMoInGH.Component;
using Grasshopper.GUI;
using Grasshopper.GUI.Canvas;
using Grasshopper.Kernel;
using System;
using System.Drawing;

namespace AiMoCore
{
    partial class GHCSharpScriptInstance
    {

        public virtual void ComponentAdditionalLayout(RectangleF bounds)
        {
        }
        public virtual void ComponentAdditionalRender(GH_Canvas canvas, Graphics graphics)
        {
        }
        public virtual bool ComponentAdditionalIsPickRegion(PointF point)
        {
            return false;
        }
        AiMoInGHComponentAttributes CurrentAttributes => (AiMoInGHComponentAttributes)this.Component.Attributes;

        public virtual GH_ObjectResponse ComponentAdditionalRespondToMouseDown(GH_Canvas sender, GH_CanvasMouseEvent e)
        {
            return CurrentAttributes.RespondToMouseDownBase(sender, e);
        }
        public virtual GH_ObjectResponse ComponentAdditionalRespondToMouseMove(GH_Canvas sender, GH_CanvasMouseEvent e)
        {
            return CurrentAttributes.RespondToMouseMoveBase(sender, e);
        }
        public virtual GH_ObjectResponse ComponentAdditionalRespondToMouseUp(GH_Canvas sender, GH_CanvasMouseEvent e)
        {
            return CurrentAttributes.RespondToMouseUpBase(sender, e);
        }

        public virtual Bitmap Icon => null;

        public Bitmap QuickIcon(string name, Color foreground, Color background, int width = 24, int height = 24)
        {
            Size sizeRaw = GH_FontServer.MeasureString(name, GH_FontServer.Standard);
            float scale = Math.Min((float)width / sizeRaw.Width, (float)height / sizeRaw.Height);
            Font font = GH_FontServer.NewFont(GH_FontServer.Standard, GH_FontServer.Standard.Size * scale);

            float x = (width - sizeRaw.Width * scale) * 0.5f;
            float y = (height - sizeRaw.Height * scale) * 0.5f;
            Bitmap bit = new Bitmap(width, height);
            using (Graphics graphics = Graphics.FromImage(bit))
            {
                graphics.FillRectangle(new SolidBrush(background), 0, 0, width, height);
                graphics.DrawString(name, font, new SolidBrush(foreground), x, y);
            }
            return bit;
        }

    }
}
